﻿@model IEnumerable<Marbale.BusinessObject.Tax.TaxStructure>
@{
    
    ViewBag.Title = "Tax Structure";
    Layout = null;
}

<div class="taxsetmodel">
    <div id="taxtable">
        <a href="#" class="btn btn-primary btn-md" role="button" onclick="AddTax()">Add Tax Structure</a>
        <table id="TaxStructure" class="table TaxStructure">
            <thead>
                <th>Tax StructureId</th>
                <th>Tax structure Name</th>
                <th>Tax structure Percentage</th>
            </thead>
            @if (Model != null)
            {
                int count = 1;
                foreach (var item1 in Model)
                {  
                <tr onchange="UpdatedList(this)" style="background-color:white">
                    <td>
                        <input type="text" id="TaxStructureId@(count)" value="@item1.TaxStructureId" disabled >
                    </td>

                    <td>
                        <input type="text" value="@item1.TaxStructureName"  id="TaxStructureName@(count)"/>
                    </td>
                    <td>
                        <input type="number" value="@item1.TaxStructurePercentage" id="TaxStructurePercentage@(count)" />
                    </td>
                </tr> 
                    count++;
                }
            }
        </table>
    </div>
    <button class="btn btn-primary btn-md" role="button" onclick="SaveTaxSet()">Save</button>
</div>
<style>

</style>
<script>
    function AddTax() {
        var index = $("#TaxStructure tbody tr").length + 1;
        var TaxStructureId = "TaxStructureId" + index;
        var TaxStructureName = "TaxStructureName" + index;
        var TaxStructurePercentage = "TaxStructurePercentage" + index;
        var newRow = '<tr onchange=UpdatedList(this) style="background-color:white"> <td> <a href="#"></a> </td>' +
     '<td> <input type="text" id="' + TaxStructureName + '" />  </td>' +
     '<td> <input type="number"  id="' + TaxStructurePercentage + '" />  </td>' +
      '</tr>';
        $('#TaxStructure tr:last').after(newRow);
    }
    function UpdatedList(x) {

        updatedArray = [];
        updatedArray.push(x.rowIndex);
    }
    function SaveTaxSet() {

        var table = document.getElementById('TaxStructure');
        var id = updatedArray[0];

        var taxstructure = {
            TaxId: localStorage['TaxId'],
            TaxStructureId: $('#TaxStructureId' + id).val(),
            TaxStructureName: $('#TaxStructureName' + id).val(),
            TaxStructurePercentage: $("#TaxStructurePercentage" + id).val()
        };
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: '@Url.Action("InsertUpdateTaxStructure", "Tax")',
            data: JSON.stringify({ taxstructure: taxstructure }),
            dataType: "json",
            success: function (data) {
                alert('Data Saved');
                location.reload();
            },
            error: function (e) {
                alert(e);
            }
        });
    }

</script>
